package com.whl.system.service.impl;

import com.whl.system.domain.Systemdictionarydetail;
import com.whl.system.domain.Systemdictionarytype;
import com.whl.system.mapper.SystemdictionarydetailMapper;
import com.whl.system.mapper.SystemdictionarytypeMapper;
import com.whl.system.service.ISystemdictionarytypeService;
import com.whl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：字典类型业务类
 */
@Service
public class SystemdictionarytypeServiceImpl extends BaseServiceImpl<Systemdictionarytype> implements ISystemdictionarytypeService {

    @Autowired
    private SystemdictionarydetailMapper systemdictionarydetailMapper;

    @Autowired
    private SystemdictionarytypeMapper systemdictionarytypeMapper;

    /**
     *  级联删除
     *  删除数据类型的同时删除数据明细
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id){
        // 自定义方法 获取明细里面的types_id相当于类型里面id的所有明细对象
        List<Systemdictionarydetail> systemDictionarydetail = systemdictionarydetailMapper.findByTypesId(id);
        // 遍历systemDictionarydetail
        for(Systemdictionarydetail dictionarydetail: systemDictionarydetail){
            // 获取对象id
            Long id1 = dictionarydetail.getId();
            // 循环删除
            systemdictionarydetailMapper.delete(id1);
        }
        // 删除完了字典明细之后再删除字典类型
        systemdictionarytypeMapper.delete(id);
    }
}
